package com.css.gdzj.gdzjbxgd.modules.reportcontral.controller;


import com.alibaba.fastjson.JSONObject;
import com.css.gdzj.gdzjbxgd.common.util.R;
import com.css.gdzj.gdzjbxgd.modules.reportcontral.entity.BxgdReportContralEntity;
import com.css.gdzj.gdzjbxgd.modules.reportcontral.service.BxgdReportContralService;
import com.css.gdzj.gdzjbxgd.modules.userinfo.repository.entity.UserInfoEntity;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 填报时间控制
 * @author zz
 */
@Slf4j
@RestController
@RequestMapping("/bxgdReportContral")
public class BxgdReportContralController {
    private Logger log  = LoggerFactory.getLogger(getClass());
    @Autowired
    private BxgdReportContralService bxgdReportContralService;

    /**
     * 获取填报时间控制
     * @param reportYear
     * @return
     */
    @GetMapping(value = "/getList")
    public R dirLaws(@RequestParam String reportYear,
                     @RequestParam(required = false) Integer pageNo,
                     @RequestParam(required = false) Integer pageSize) {
        try{
            PageRequest pageRequest = PageRequest.of(pageNo-1,pageSize);
            Map<String,Object> map = new HashMap<>();
            map = bxgdReportContralService.getList(reportYear,pageNo,pageSize);
            List<BxgdReportContralEntity> list = new ArrayList<>();
            list = (List) map.get("list");
            int total= (int) map.get("total");
            Integer totalPage = 1;
            if (pageSize > 0 && total > 0 ){
                if (pageSize >= total){
                    totalPage = 1;
                }else {
                    totalPage = total/pageSize;
                    if (total % pageSize != 0){
                        totalPage ++;
                    }
                }
            }
            return R.ok().put("data",list).put("total",total).put("totalPage",totalPage);
    }catch (Exception e){
        String errorMessage = "查询失败!";
        log.error(errorMessage,e);
        return R.error();
    }
    }
    @PostMapping("/addBxgdReportContral")
    public R saveBxgdReportContral(@RequestBody BxgdReportContralEntity bxgdReportContralEntity) {
        try{
            bxgdReportContralEntity.setReportTimeEnd(bxgdReportContralEntity.getReportTimeEnd().concat(" 23:59:59")) ;
            bxgdReportContralService.saveBxgdReportContral(bxgdReportContralEntity);
            return R.ok();
        }catch (Exception e){
            String errorMessage = "新增失败!";
            log.error(errorMessage,e);
            return R.error();
        }


    }
    @PostMapping("/updateBxgdReportContral")
    public R updateBxgdReportContral(@RequestBody BxgdReportContralEntity bxgdReportContralEntity) {
        try{
            bxgdReportContralService.updateBxgdReportContral(bxgdReportContralEntity);
            return R.ok();
        }catch (Exception e){
            String errorMessage = "修改失败!";
            log.error(errorMessage,e);
            return R.error();
        }


    }

    @PostMapping("/deleteBxgdReportContral")
    public R deleteBxgdReportContral(@RequestBody Map<String, Object> params) {
        try{
            List<String> idList = new ArrayList<>();
            idList = (List<String>) params.get("ids");
            bxgdReportContralService.deleteBxgdReportContral(idList);
            return R.ok();
        }catch (Exception e){
            String errorMessage = "删除失败!";
            log.error(errorMessage,e);
            return R.error();
        }
    }

}
